//
//  MoreProductViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/28.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "MoreProductViewController.h"

#import "OrderPaperBoardViewController.h"
#import "SelectBoxModelController.h"
#import "PackageCell.h"

@interface MoreProductViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *recommendProductArray;

@end

@implementation MoreProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.recommendProductArray = [NSMutableArray array];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = ScreenX375(108);
    [self.view addSubview:self.tableView];
    
    [self getProduct];
}

- (void)getProduct {
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/recommend_product",REQUESTHEADER] andParameter:@{@"factory_id":kNSUDefaultReadKey(@"FactoryId")} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [self.recommendProductArray removeAllObjects];
            NSArray *arr = [[successResponse[@"data"] reverseObjectEnumerator] allObjects];
            for (NSDictionary *dict in arr) {
                CHCardItemModel *model = [[CHCardItemModel alloc] initWithDic:dict];
                [self.recommendProductArray addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _recommendProductArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PackageCell *cell = [PackageCell cellWithTableView:tableView andIndexPath:indexPath];
    [cell initWithCardModel:_recommendProductArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CHCardItemModel *model = self.recommendProductArray[indexPath.row];
    if ([model.goods_type integerValue] == 2) {
        OrderPaperBoardViewController *vc = [[OrderPaperBoardViewController alloc] init];
        vc.productId = model.cardId;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        SelectBoxModelController *vc = [[SelectBoxModelController alloc] init];
        vc.producId = model.cardId;
        vc.isPaperBox = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
